﻿#include <QFile>
#include "CCotdetQueryDlg.h"
#include "CHeader.h"
#include "CImgShowDlg.h"
#include "CItemDetailDlg.h"
#include "CPanItemDetailDlg.h"
#include <QSqlRecord>
#include "CSqlOper.h"
CCotdetQueryDlg::CCotdetQueryDlg(CRBaseData* pBasicData, QString sSceneGuid,QString sTaskExecGuid, QString sTaskName, int nItemTypeCode, QWidget *parent)
	:m_pBasicData(pBasicData), m_sSceneGuid(sSceneGuid),m_sTaskExecGuid(sTaskExecGuid),m_sTaskName(sTaskName),m_nItemTypeCode(nItemTypeCode),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setStyleSheetFile();
	initConnect();
	initData();
	initUI();
	queryItem();
}

CCotdetQueryDlg::~CCotdetQueryDlg()
{}

void CCotdetQueryDlg::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("持续监测巡检项结果"));
	QString sQss = setUiStyle("styleSheet");
	QString sMyqss = styleSheet();
	QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	setStyleSheet(sMerged);
}

void CCotdetQueryDlg::initConnect()
{
	connect(ui.widget, SIGNAL(sigShowItem(QString, QString, QString, QString, int, int, int)), this, SLOT(slotShowItem(QString, QString, QString, QString, int, int, int)));
	//connect(ui.widget, SIGNAL(sigShowImg(QString)), this, SLOT(slotShowImg(QString)));
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CCotdetQueryDlg::slotClose);
}

void CCotdetQueryDlg::initUI()
{
	m_mapItemDeviceType.insert(enCamer, A2T("图像相机"));
	m_mapItemDeviceType.insert(enIR, A2T("红外相机"));
	m_mapItemDeviceType.insert(enVoice, A2T("声音传感器"));
	m_mapItemDeviceType.insert(enCO, A2T("一氧化碳"));
	m_mapItemDeviceType.insert(enGas, A2T("可燃气体"));
	m_mapItemDeviceType.insert(enPM25, A2T("PM2.5"));
	m_mapItemDeviceType.insert(enPM10, A2T("PM10"));
	m_mapItemDeviceType.insert(enTemp, A2T("温度传感器"));
	m_mapItemDeviceType.insert(enHum, A2T("湿度传感器"));

	m_mapUnit.insert(enIR, A2T("℃"));
	m_mapUnit.insert(enVoice, A2T("db"));
	m_mapUnit.insert(enCO, A2T("ppm"));
	m_mapUnit.insert(enGas, A2T("ppm"));
	m_mapUnit.insert(enPM25, A2T("ug/m3"));
	m_mapUnit.insert(enPM10, A2T("ug/m3"));
	m_mapUnit.insert(enTemp, A2T("℃"));
	m_mapUnit.insert(enHum, A2T("%RH"));
	m_mapUnit.insert(enCamer, A2T(""));
	m_mapUnit.insert(enUltrasonic, A2T(""));

	//初始化tablewidget
	ui.widget->InitUi(2);
}

void CCotdetQueryDlg::initData()
{
	//巡检项名称传感器
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectPloItemSensorBySceneID(m_sSceneGuid, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QString sGuid = vecRecord[i].value("GUID").toString();
			QString sName = vecRecord[i].value("POLL_ITEM_NAME").toString();
			m_mapItemName.insert(sGuid, sName);
		}
	}

	//巡检项名称云台
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectPloItemCamerBySceneID(m_sSceneGuid, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QString sGuid = vecRecord[i].value("GUID").toString();
			QString sName = vecRecord[i].value("POLL_ITEM_NAME").toString();
			m_mapItemName.insert(sGuid, sName);
		}
	}

	//巡检点名称
	{
		m_pBasicData->m_pMutex->lock();
		for (QMap<QString, CPolPoint*>::iterator itor = m_pBasicData->m_pPolArea->m_mapPolPoint.begin();
			itor != m_pBasicData->m_pPolArea->m_mapPolPoint.end(); itor++)
		{
			CPolPoint* pPoint = itor.value();
			m_mapPointName.insert(pPoint->m_sPointGuid, pPoint->m_sPointName);
			m_mapPointLoc.insert(pPoint->m_sPointGuid, pPoint->m_dPointLocation);
		}
		m_pBasicData->m_pMutex->unlock();
	}
}

void CCotdetQueryDlg::queryItem()
{
	QVector<QVector<QString>>vecStr;
	int nRow = 0;
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectImageItemExecCotdet(m_sTaskExecGuid,m_nItemTypeCode, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QVector<QString> vecRow;
			vecRow.push_back(QString::number(nRow));//序号
			vecRow.push_back(m_sTaskName);//任务名称
			vecRow.push_back(m_mapPointName[vecRecord[i].value("POINT_GUID").toString()]);//巡检点名称
			vecRow.push_back(vecRecord[i].value("ROLLER_CODE").toString());//托辊号
			vecRow.push_back(m_mapItemName[vecRecord[i].value("ITEM_GUID").toString()]);//巡检项名称
			vecRow.push_back(QString::number(1));//设备类型1：云台
			vecRow.push_back(m_mapItemDeviceType[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//巡检类型
			vecRow.push_back(vecRecord[i].value("START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//执行时间
			if (vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enIR)//红外相机
			{
				vecRow.push_back(vecRecord[i].value("TEMP_MAX").toString());//执行结果，使用最高温作为执行结果
				vecRow.push_back(vecRecord[i].value("DATA_THRESHOLD_H").toString());//报警阀值
			}
			else if (vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enVoice)//声音传感器
			{
				vecRow.push_back(vecRecord[i].value("VOICE_DATA").toString());//执行结果，使用最高温作为执行结果
				vecRow.push_back(vecRecord[i].value("DATA_THRESHOLD_H").toString());//报警阀值
			}
			else if (vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enCamer)//图像相机
			{
				vecRow.push_back("/");//执行结果，使用最高温作为执行结果
				vecRow.push_back("/");//报警阀值
			}
			else 
			{
				continue;//其他类型的暂时不显示
			}
			vecRow.push_back(vecRecord[i].value("RESULT_HTTP_URL").toString());//结果图片
			vecRow.push_back(vecRecord[i].value("GUID").toString());//巡检项执行记录Guid
			vecRow.push_back(QString::number(m_mapPointLoc[vecRecord[i].value("POINT_GUID").toString()]));
			vecRow.push_back(vecRecord[i].value("IS_COTDET").toString());//是否连续检测型
			vecRow.push_back(vecRecord[i].value("ITEM_TYPE_CODE").toString());//巡检项类型编码
			vecRow.push_back(m_mapUnit[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//对应的单位
			vecRow.push_back(QString::number(0));
			vecStr.push_back(vecRow);
			nRow++;
		}
		/*for (int i = 0; i < vecRecord.size(); i++)
		{
			QVector<QString> vecRow;
			vecRow.push_back(QString::number(nRow));//序号
			vecRow.push_back(m_sTaskName);//任务名称
			vecRow.push_back(m_mapItemDeviceType[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//巡检类型
			vecRow.push_back(vecRecord[i].value("START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//报警时间
			if (m_nItemTypeCode == enIR)//红外的
			{
				vecRow.push_back(vecRecord[i].value("TEMP_MAX").toString()+ m_mapUnit[enIR]);//执行结果
			}
			else if (m_nItemTypeCode == enVoice)//声音的
			{
				vecRow.push_back(vecRecord[i].value("VOICE_DATA").toString() + m_mapUnit[enVoice]);//执行结果
			}
			else 
			{
				vecRow.push_back("");//执行结果
			}
			vecRow.push_back(vecRecord[i].value("DATA_THRESHOLD_H").toString() + m_mapUnit[m_nItemTypeCode]);//报警阀值
			vecRow.push_back(vecRecord[i].value("COTDET_LOC").toString()+"m");//报警所在位置
			vecRow.push_back(vecRecord[i].value("COTDET_ROLLER_CODE").toString());//报警所在托辊号
			vecRow.push_back(vecRecord[i].value("RESULT_HTTP_URL").toString());//结果图片
			vecStr.push_back(vecRow);
			nRow++;
		}*/
	}
	ui.widget->InitData(2, vecStr);
}

void CCotdetQueryDlg::slotShowItem(QString sTaskName, QString sItemExecGuid, QString sItemName, QString sPointName, int nDeviceType, int nIsCotdet, int nItemTypeCode)
{
	if (nDeviceType == 0)//传感器
	{
		CItemDetailDlg oper(sItemExecGuid, sItemName, sPointName);
		oper.exec();
	}
	else if (nDeviceType == 1)//云台
	{
		CPanItemDetailDlg oper(sItemExecGuid, sItemName, sPointName);
		oper.exec();
	}
}

void CCotdetQueryDlg::slotShowImg(QString sHttpUrl)
{
	CImgShowDlg oper(sHttpUrl);
	oper.exec();
}

void CCotdetQueryDlg::slotClose()
{
	this->close();
}
